package com.example.photographWatermark_example.camera2

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.example.photographWatermark_example.R
import kotlinx.android.synthetic.main.activity_camera2.*

/**
 * author :  chensen
 * data  :  2018/3/18
 * desc :
 */

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class CameraActivity : AppCompatActivity() {

    private lateinit var mCamera2Helper: CameraHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camera2)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        val bean = CameraBean("广东省惠州市惠城区鹅岭南路6号TCL工业大厦", "林小姐", "11:08 2020-10-10 星期一")
        mCamera2Helper = CameraHelper(this, textureView, imageCallback, bean)

        btnTakePic.setOnClickListener {
            mCamera2Helper.takePic()
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        mCamera2Helper.releaseCamera()
        mCamera2Helper.releaseThread()
    }

    private val imageCallback: ImageCallback = object : ImageCallback {

        override fun onSuccess(bitmapPath: String) {
            Log.d("----", "图片返回成功：${bitmapPath}")
            runOnUiThread {
                val intent = Intent()
                intent.action = "main"
//                intent.setData()
//                val bean = CameraBean("aaa",bitmapPath,"2012")
                intent.putExtra("camera", bitmapPath)
                startActivity(intent)
            }

        }

    }
//    private val onImageAvailableListener = ImageReader.OnImageAvailableListener {
//
//        val image = it.acquireNextImage()
//        val byteBuffer = image.planes[0].buffer
//        val byteArray = ByteArray(byteBuffer.remaining())
//        byteBuffer.get(byteArray)
//        it.close()
//        Log.d("---", "图片保存")
//
//        val temp = System.currentTimeMillis()
//        val picFile = FileUtil.createCameraFile("camera2")
//        if (picFile != null && byteArray != null) {
//
//            val rawBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
//            val resultBitmap = if (isMirror) BitmapUtils.mirror(rawBitmap) else rawBitmap
//
//
//            picFile.sink().buffer().write(BitmapUtils.toByteArray(resultBitmap)).close()
//
//        }
//    }
}

